/**
 * @author Tobias Boehm
 *
 * 28.04.2011
 */
package org.anddev.andengine.extension.tbsengine.map;

import org.anddev.andengine.entity.layer.tiled.tmx.util.constants.TMXConstants;
import org.anddev.andengine.util.SAXUtils;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


/**
 * This is a basic implementation of a map overview parser. It takes an empty 
 * map overview object and fills it little-by-little by traversing the map file.
 * <p>
 * You will use an instance of this class inside your implementation of
 * {@link AbsMapOverviewLoader}.
 * 
 * @see DefaultHandler
 * 
 * @author Tobias Boehm
 *
 */
public class BaseMapOverviewParser<MapOverviewClass extends AbsMapOverview> extends DefaultHandler implements TMXConstants, TBSmapConstants {
	
	/**
	 * The map overview object which will be filled.
	 */
	private final MapOverviewClass mMapOverview;

	/**
	 * Flag that indicates that the parser is inside the 
	 * {@link TMXConstants#TAG_MAP} element.
	 */
	private boolean mInMap;
	
	/**
	 * Flag that indicates that the parser is
	 * inside the {@link TMXConstants#TAG_PROPERTIES} element.
	 */
	private boolean mInProperties;
	
	
	/**
	 * This is the default constructor of this class. It takes an empty map 
	 * overview object as parameter and stores it.
	 * 
	 * @param pMapOverview - An empty map overview object.
	 */
	public BaseMapOverviewParser(final MapOverviewClass pMapOverview){
	
		this.mMapOverview = pMapOverview;
	}
	
	/**
	 * This method returns the map overview object which was provide through the
	 * constructor.
	 * 
	 * @return - The {@link #mMapOverview} object.
	 */
	public MapOverviewClass getTBSMapOverview(){
				
		return this.mMapOverview;
	}
	
	@Override
	public void startElement(final String pUri, final String pLocalName, final String pQualifiedName, final Attributes pAttributes) throws SAXException {
		
		if(pLocalName.equals(TAG_MAP)){
		
			this.mInMap = true;
			
			// Add map property attributes
			this.mMapOverview.addTMXTiledMapProperty(pAttributes);
		
		} else if(pLocalName.equals(TAG_PROPERTIES)) {
			
			this.mInProperties = true;
		
		} else if(this.mInProperties && pLocalName.equals(TAG_PROPERTY)) {
					
			if(this.mInMap) {			
				
				// Add map property attribute
				this.mMapOverview.addTMXTiledMapProperty(pAttributes);			
			}
			
		} else if(pLocalName.equals(TAG_OBJECT)){
			
			// Get object type
			final String objectType = SAXUtils.getAttributeOrThrow(pAttributes, BaseGameObjectPropertyField.type.name());
			
			// If the current object is a starting point increase possible player count
			if((objectType != null) && objectType.equals(BaseMapObjectPropertyValue.startingpoint.name())){
				
				this.mMapOverview.setPlayerCount(this.mMapOverview.getPlayerCount() + 1);
			}	
		}
	}
	
	@Override
	public void endElement(final String pUri, final String pLocalName, final String pQualifiedName) throws SAXException {
		
		if(pLocalName.equals(TAG_MAP)){
		
			this.mInMap = false;
		
		} else if(pLocalName.equals(TAG_PROPERTIES)) {
		
			this.mInProperties = false;		
		}
	}
}